package com.aidex.generator.domain;

import com.aidex.common.constant.GenConstants;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.utils.StringUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ArrayUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel("业务表")
public class GenTable extends BaseEntity {

    @ApiModelProperty("表名称")
    @NotBlank(message = "表名称不能为空")
    private String tableName;

    @ApiModelProperty("表描述")
    @NotBlank(message = "表描述不能为空")
    private String tableComment;

    @ApiModelProperty("关联父表的表名")
    private String subTableName;

    @ApiModelProperty("本表关联父表的外键名")
    private String subTableFkName;

    @ApiModelProperty("实体类名称(首字母大写)")
    @NotBlank(message = "实体类名称不能为空")
    private String className;

    @ApiModelProperty("使用的模板（crud单表操作 tree树表操作 sub主子表操作）")
    private String tplCategory;

    @ApiModelProperty("使用的模板（crud单表操作 tree树表操作 sub主子表操作）")
    @TableField(exist = false)
    private String parentTplCategory;

    @ApiModelProperty("生成包路径")
    @NotBlank(message = "生成包路径不能为空")
    private String packageName;

    @ApiModelProperty("生成模块名")
    @NotBlank(message = "生成模块名不能为空")
    private String moduleName;

    @ApiModelProperty("生成业务名")
    @NotBlank(message = "生成业务名不能为空")
    private String businessName;

    @ApiModelProperty("生成功能名")
    @NotBlank(message = "生成功能名不能为空")
    private String functionName;

    @ApiModelProperty("生成作者")
    private String functionAuthor;

    @ApiModelProperty("邮箱")
    private String functionAuthorEmail;

    @ApiModelProperty("工作空间路径")
    private String workspacePath;

    @ApiModelProperty("前端工作空间路径")
    private String webWorkspacePath;

    @ApiModelProperty("生成代码方式（0zip压缩包 1自定义路径）")
    private String genType;

    @ApiModelProperty("列数")
    private String colSpans;

    @ApiModelProperty("主键信息")
    @TableField(exist = false)
    private GenTableColumn pkColumn;

    @ApiModelProperty("子表信息")
    @TableField(exist = false)
    private GenTable subTable;

    @Valid
    @ApiModelProperty("表列信息")
    @TableField(exist = false)
    private List<GenTableColumn> columns;

    @ApiModelProperty("其它生成选项")
    private String options;

    @ApiModelProperty("树编码字段")
    @TableField(exist = false)
    private String treeCode;

    @ApiModelProperty("树父编码字段")
    @TableField(exist = false)
    private String treeParentCode;

    @ApiModelProperty("树名称字段")
    @TableField(exist = false)
    private String treeName;

    @ApiModelProperty("上级菜单ID字段")
    @TableField(exist = false)
    private String parentMenuId;

    @ApiModelProperty("上级菜单名称字段")
    @TableField(exist = false)
    private String parentMenuName;

    @ApiModelProperty("菜单icon")
    @TableField(exist = false)
    private String menuIcon;

    @ApiModelProperty("是否上传附件")
    @TableField(exist = false)
    private String attachOption;

    @ApiModelProperty("是否有停用启用")
    @TableField(exist = false)
    private String disableEnableOption;

    @ApiModelProperty("是否保存并继续添加")
    @TableField(exist = false)
    private String saveAndAddOption;

    @ApiModelProperty("是否有复制功能")
    @TableField(exist = false)
    private String copyRecordOption;

    @ApiModelProperty("menuID")
    @TableField(exist = false)
    private String genCodeMenuId;

    public Boolean isSub() {
        return isSub(this.tplCategory);
    }

    public static Boolean isSub(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_SUB, tplCategory);
    }

    public Boolean isTree() {
        return isTree(this.tplCategory);
    }

    public static Boolean isTree(String tplCategory) {
        return tplCategory != null && (StringUtils.equals(GenConstants.TPL_TREE, tplCategory) || StringUtils.equals(GenConstants.TPL_TREEGRID, tplCategory));
    }

    public Boolean isTreegrid() {
        return isTreegrid(this.tplCategory);
    }

    public static Boolean isTreegrid(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREEGRID, tplCategory);
    }

    public Boolean isCrudRowEdit() {
        return isCrudRowEdit(this.tplCategory);
    }

    public static Boolean isCrudRowEdit(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD_ROW_EDIT, tplCategory);
    }

    public Boolean isCrud() {
        return isCrud(this.tplCategory);
    }

    public static Boolean isCrud(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory);
    }

    public Boolean isSuperColumn(String javaField) {
        return isSuperColumn(this.tplCategory, javaField);
    }

    public static Boolean isSuperColumn(String tplCategory, String javaField) {
        if (isTree(tplCategory) || isTreegrid(tplCategory)) {
            return StringUtils.equalsAnyIgnoreCase(javaField,
                    ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
        }
        return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
    }

    public Boolean hasAttach() {
        return hasAttach(this.attachOption);
    }

    public Boolean hasAttach(String attachOption) {
        return attachOption != null && StringUtils.equals("1", attachOption);
    }

    public Boolean hasDisableEnable() {
        return hasDisableEnable(this.disableEnableOption);
    }

    public Boolean hasDisableEnable(String disableEnableOption) {
        return disableEnableOption != null && StringUtils.equals("1", disableEnableOption);
    }

    public Boolean hasSaveAndAddOption() {
        return hasSaveAndAddOption(this.saveAndAddOption);
    }

    public Boolean hasSaveAndAddOption(String saveAndAddOption) {
        return saveAndAddOption != null && StringUtils.equals("1", saveAndAddOption);
    }

    public Boolean hasCopyRecordOption() {
        return hasCopyRecordOption(this.copyRecordOption);
    }

    public Boolean hasCopyRecordOption(String copyRecordOption) {
        return copyRecordOption != null && StringUtils.equals("1", copyRecordOption);
    }
}